(function($){
    $(document).ready(function () {
        /*模板引擎设置*/
        _.templateSettings = {
            interpolate: /\{\{(.+?)\}\}/g
        };

        var InvoiceModel=Backbone.Model.extend({
            workflow:{
                initial:"draft",
                events:[
                    {name:"issue",from:"draft",to:"issued"},
                    {name:"payout",from:"issued",to:"paid"},
                    {name:"cancel",from:"paid",to:"canceled"},
                    {name:"post",from:"paid",to:"success"},
                    {name:"issue",from:"draft",to:"issued"}
                ]

            },

            defaults:{
                price:0,
                quantity:0
            },


            initialize:function(){
                _.extend(this,new Backbone.Workflow(this,{attrName:"status"}));

                this.bind("transition:from:draft",function(){
                    this.set("createdDate",new Date().toISOString());
                });

                this.bind("transition:to:canceled",function(){
                    this.set("cancelDate",new Date().toISOString());
                });


            }
        });

        var invoiceModel=new InvoiceModel();
        console.log(invoiceModel.get("status"));
        invoiceModel.triggerEvent("issue");
        console.log(">>",invoiceModel.get("createdDate"));
        console.log(invoiceModel.get("status"));
        invoiceModel.triggerEvent("payout");
        console.log(invoiceModel.get("status"));

       /* invoiceModel.triggerEvent("post");
        console.log(invoiceModel.get("status"));*/

        invoiceModel.triggerEvent("cancel");
        console.log(">>",invoiceModel.get("cancelDate"));
        console.log(invoiceModel.get("status"));

    });
})(jQuery);